package com.jyy.prefabricated.service;

import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.ProjectDto;
import com.jyy.prefabricated.dto.PurchaseItemsDto;
import com.jyy.prefabricated.dto.StudentSignUpApplyDto;
import com.jyy.prefabricated.entity.*;
import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.enums.CourseTypeEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.ProjectInfoCourseMapper;
import com.jyy.prefabricated.mapper.ProjectInfoMapper;
import com.jyy.prefabricated.mapper.ProjectInfoQuestionBankMapper;
import com.jyy.prefabricated.mapper.StudentSignUpApplyMapper;
import com.jyy.prefabricated.vo.StudentSignUpApplyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class ProjectInfoService {

    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    @Autowired
    private ProjectInfoCourseMapper projectInfoCourseMapper;

    @Autowired
    private ProjectCategoryService projectCategoryService;

    @Autowired
    private ProjectInfoQuestionBankMapper projectInfoQuestionBankMapper;

    @Autowired
    private StudentSignUpApplyMapper applyMapper;
    /**
     * 新增
     * @param dto
     * @return
     */
    public int insert(ProjectDto dto){
        ProjectInfo projectInfo = new ProjectInfo();
        BeanUtils.copyProperties(dto,projectInfo);
        projectInfo.setStatus(BaseStatusEnum.INVALID.getValue());
        projectInfoMapper.insert(projectInfo);
        //保存项目课程
        saveProjectInfoCourse(dto, projectInfo.getId());
        //保存项目题库
        saveProjectInfoQuestionBank(dto, projectInfo.getId());
        return projectInfo.getId();
    }

    /**
     * 保存项目课程
     * @param dto
     * @param projectId
     */
    private void saveProjectInfoCourse(ProjectDto dto, Integer projectId){
        List<ProjectInfoCourse> projectInfoCourses = new ArrayList<>();
        for(Integer courseId : dto.getDocumentCourseIds()){
            ProjectInfoCourse course = new ProjectInfoCourse();
            course.setCourseId(courseId);
            course.setProjectId(projectId);
            course.setType(CourseTypeEnum.DOCUMENT.getValue());
            projectInfoCourses.add(course);
        }
        for(Integer courseId : dto.getRecordingCourseIds()){
            ProjectInfoCourse course = new ProjectInfoCourse();
            course.setCourseId(courseId);
            course.setProjectId(projectId);
            course.setType(CourseTypeEnum.RECORDING.getValue());
            projectInfoCourses.add(course);
        }
        if(projectInfoCourses.size() > 0){
            projectInfoCourseMapper.insert(projectInfoCourses);
        }
    }

    /**
     * 批量更新状态
     * @param dto
     * @return
     */
    public int batchUpdateStatus(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return projectInfoMapper.batchUpdateStatus(dto.getIds(), dto.getStatus());
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    public int batchDelete(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return projectInfoMapper.batchDelete(dto.getIds());
    }


    /**
     * 保存项目题库
     * @param dto
     * @param projectId
     */
    private void saveProjectInfoQuestionBank(ProjectDto dto, Integer projectId){
        List<ProjectInfoQuestionBank> projectInfoQuestionBanks = new ArrayList<>();
        for(Integer bankId : dto.getQuestionBankIds()){
            ProjectInfoQuestionBank questionBank = new ProjectInfoQuestionBank();
            questionBank.setProjectId(projectId);
            questionBank.setQuestionBankId(bankId);
            questionBank.setQuestionBankId(bankId);
            projectInfoQuestionBanks.add(questionBank);
        }
        if(projectInfoQuestionBanks.size()>0){
            projectInfoQuestionBankMapper.insert(projectInfoQuestionBanks);
        }
    }

    /**
     * 更新
     * @param dto
     * @return
     */
    public int update(ProjectDto dto){
        if(dto.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        ProjectInfo projectInfo = new ProjectInfo();
        BeanUtils.copyProperties(dto,projectInfo);
        int row = projectInfoMapper.updateByPrimaryKeySelective(projectInfo);

        //保存项目课程
        projectInfoCourseMapper.deleteByProjectId(dto.getId());
        saveProjectInfoCourse(dto, projectInfo.getId());
        //保存项目题库
        projectInfoQuestionBankMapper.deleteByProjectId(dto.getId());
        saveProjectInfoQuestionBank(dto, projectInfo.getId());
        return row;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        ProjectInfo projectInfo = projectInfoMapper.selectByPrimaryKey(id);
        projectInfo.setDeleted(true);
        return projectInfoMapper.updateByPrimaryKeySelective(projectInfo);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public ProjectInfo selectById(Integer id){
        return projectInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据参数查询列表
     * @param categoryId 分类id
     * @param name 名称
     * @param status 状态
     * @return
     */
    public List<ProjectInfo> selectByParams(Integer categoryId, String name, Integer status){
        return projectInfoMapper.selectByParams(categoryId, name, status);
    }

    /**
     * 根据参数查询列表
     * @param categoryIds 分类id列表
     * @param name 名称
     * @param status 状态
     * @return
     */
    public List<PurchaseItemsDto> selectPurchaseItems(List<Integer> categoryIds, String name, Integer status){
        List<PurchaseItemsDto> resultPurchaseItemsDto= projectInfoMapper.selectPurchaseItems(categoryIds, name,status);
        for (PurchaseItemsDto purchaseItemsDto:resultPurchaseItemsDto) {
            StudentSignUpApplyDto studentSignUpApplyDto = new StudentSignUpApplyDto();
            studentSignUpApplyDto.setOrderId(purchaseItemsDto.getOrderId());
            List<StudentSignUpApplyVo> studentSignUpApplyVos = applyMapper.selectByParam(studentSignUpApplyDto);
            if(studentSignUpApplyVos != null){
                if(studentSignUpApplyVos.get(0).getRegistrationType() == 1){
                    purchaseItemsDto.setBuyer(studentSignUpApplyVos.get(0).getName());
                }else{
                    purchaseItemsDto.setBuyer(studentSignUpApplyVos.get(0).getTrainingOrganizationName());
                }
                int oneWcount = 0;
                int oneYcount = 0;
                int twoWcount = 0;
                int twoYcount = 0;
                for (StudentSignUpApplyVo studentSignUpApplyVo:studentSignUpApplyVos) {
                        /*if(studentSignUpApplyVo.getPayType() == 2){
                            ycount++;
                        }
                        if(studentSignUpApplyVo.getPayType() == 0){
                            wcount++;
                        }*/
                    if(Integer.valueOf(1).equals(studentSignUpApplyVo.getRefundAudit()) || Integer.valueOf(3).equals(studentSignUpApplyVo.getRefundAudit())){
                        oneYcount++;
                    }else if(Integer.valueOf(1).equals(studentSignUpApplyVo.getPayType()) || Integer.valueOf(3).equals(studentSignUpApplyVo.getPayType())){

                    }else{
                        oneWcount++;
                    }
                    if(Integer.valueOf(2).equals(studentSignUpApplyVo.getRefundAudit()) || Integer.valueOf(3).equals(studentSignUpApplyVo.getRefundAudit())){
                        twoYcount++;
                    }else if(Integer.valueOf(2).equals(studentSignUpApplyVo.getPayType()) || Integer.valueOf(3).equals(studentSignUpApplyVo.getPayType())){

                    }else{
                        twoWcount++;
                    }
                }
                if(oneWcount == studentSignUpApplyVos.size()){
                    purchaseItemsDto.setOneRefund(0);
                } else if(oneYcount == (studentSignUpApplyVos.size()-oneWcount)){
                    purchaseItemsDto.setOneRefund(2);
                } else{
                    purchaseItemsDto.setOneRefund(1);
                }
                if(twoWcount == studentSignUpApplyVos.size()){
                    purchaseItemsDto.setTwoRefund(0);
                } else if(twoYcount == (studentSignUpApplyVos.size()-twoWcount)){
                    purchaseItemsDto.setTwoRefund(2);
                } else{
                    purchaseItemsDto.setTwoRefund(1);
                }
                purchaseItemsDto.setId(studentSignUpApplyVos.get(0).getId());
                purchaseItemsDto.setStatus(studentSignUpApplyVos.get(0).getStatus());
                purchaseItemsDto.setType(studentSignUpApplyVos.get(0).getRegistrationType());
                purchaseItemsDto.setUpdateTime(studentSignUpApplyVos.get(0).getUpdateTime());

            }
        }
        return resultPurchaseItemsDto;
    }
    /**
     * 根据参数查询列表
     * @param categoryIds 分类id列表
     * @param name 名称
     * @param status 状态
     * @return
     */
    public List<ProjectInfo> selectDeepByParams( List<Integer> categoryIds, Integer type, String name, Integer status, String orderStr, Integer deadlineType){

        return projectInfoMapper.selectByParamsDeep(categoryIds, type, name,status,orderStr,deadlineType);
    }
    /**
     * 根据分类上下级关系查询分类列表
     * @param categoryId
     * @return
     */
    public List<Integer> selectDeepCategoryIds(Integer categoryId){
        List<Integer> categoryIds = new ArrayList<>();
        if(categoryId != null){
            List<ProjectCategory> list = projectCategoryService.selectAll();
            List<ProjectCategory> categories = projectCategoryService.findCategoryIdByPid(categoryId, list);
            categoryIds.addAll(categories.stream().map(e->e.getId()).collect(Collectors.toList()));
            categoryIds.add(categoryId);
        }
        return categoryIds;
    }
}
